[assembly: WebActivator.PreApplicationStartMethod(typeof(TeamsPortal.Web.Output2.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(TeamsPortal.Web.Output2.App_Start.NinjectWebCommon), "Stop")]

namespace TeamsPortal.Web.Output2.App_Start
{
    using System;
    using System.Web;
    using System.Web.Http;
    using System.Web.Mvc;
    using Microsoft.Web.Infrastructure.DynamicModuleHelper;
    using Ninject;
    using Ninject.Web.Common;
    using Ninject.Web.Mvc.FilterBindingSyntax;
    using TeamsPortal.Configuration;
    using TeamsPortal.Localization;
    using TeamsPortal.Security;
    using TeamsPortal.ServiceClient;
    using TeamsPortal.Web.Api;
    using TeamsPortal.Web.Controllers.Filters;
    using TeamsPortal.Web.Extensions.FormsAuthenticationHelper;
    using TeamsPortal.Web.Extensions.FormsAuthenticationHelper.Impl;

    public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }
        
        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }
        
        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
            RegisterServices(kernel);
            GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IMessagesServiceClient>().To<MessagesServiceClient>();
            kernel.Bind<ITopicsServiceClient>().To<TopicsServiceClient>();
            kernel.BindFilter<AddVisitAttribute>(FilterScope.Action, 0)
                .WhenActionMethodHas<AddVisitAttribute>();
            kernel.Bind<IMembershipService>().To<MembershipService>();
            kernel.Bind<IFormsAuthentication>().To<FormsAuthenticationService>();
            kernel.Bind<IUsersServiceClient>().To<UsersServiceClient>();
            kernel.Bind<IForumsServiceClient>().To<ForumsServiceClient>();
            kernel.Bind<ITopicsSubscriptionsServiceClient>().To<TopicsSubscriptionsServiceClient>();
            kernel.Bind<ILocalizer>().To<Localizer>()
                .InSingletonScope()
                .WithConstructorArgument("cultureFilePath", SiteConfiguration.Current.LocalizationFilePath);
            kernel.Bind<IFileSystemProvider>().To<FileSystemProvider>();
            kernel.Bind<IFileStorageService>().To<FileStorageService>()
                .InSingletonScope()
                .WithConstructorArgument("fileStoragePath", SiteConfiguration.Current.FileStoragePath);
            kernel.Bind<IImageService>().To<ImageService>();
            kernel.Bind<ImageController>().ToSelf();
            kernel.Bind<IRoleService>().To<RoleService>();
            kernel.Bind<RolesController>().ToSelf();
        }
    }
}
